package cn.winwintech.screenlocker

import android.content.Intent
import android.os.Bundle
import android.text.Html
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.core.net.toUri

class PrivacyActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // 检查是否已经同意隐私政策
        val prefs = getSharedPreferences("privacy", MODE_PRIVATE)
        val agreed = prefs.getBoolean("agreed", false)
        
        setContentView(R.layout.activity_privacy)

        val tvPrivacyContent = findViewById<TextView>(R.id.tvPrivacyContent)
        val btnAgree = findViewById<Button>(R.id.btnAgree)
        val btnDisagree = findViewById<Button>(R.id.btnDisagree)

        val message = """
            欢迎使用自律锁App。本App无需联网，无需登录，不搜集任何个人信息。<br><br>
            但是锁屏需要设备管理等高级权限，这是本软件实现功能锁必须的权限。作为开发者我承诺这些权限仅用于锁屏而无任何其他用途。<br><br>
            代码源码开源于：<a href="https://gitee.com/dewei-software-studio/screenlocker">gitee</a><br>
            完整的隐私政策文件请看：<a href="https://www.winwintech.cn/#/pages/privacy/privacy">隐私政策</a>
        """.trimIndent()

        tvPrivacyContent.text = Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY)
        tvPrivacyContent.movementMethod = LinkMovementMethod.getInstance()

        // 自定义链接点击处理
        val spannedText = tvPrivacyContent.text
        if (spannedText is android.text.Spanned) {
            val spans = spannedText.getSpans(0, spannedText.length, URLSpan::class.java)
            val spannable = android.text.SpannableString(spannedText)

            for (span in spans) {
                val start = spannedText.getSpanStart(span)
                val end = spannedText.getSpanEnd(span)
                val url = span.url

                spannable.removeSpan(span)
                spannable.setSpan(object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        val browserIntent = Intent(Intent.ACTION_VIEW, url.toUri())
                        startActivity(browserIntent)
                    }
                }, start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }

            tvPrivacyContent.text = spannable
        }

        btnAgree.setOnClickListener {
            val prefs = getSharedPreferences("privacy", MODE_PRIVATE)
            prefs.edit() { putBoolean("agreed", true) }
            
            // 启动 MainActivity
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }

        btnDisagree.setOnClickListener {
            val prefs = getSharedPreferences("privacy", MODE_PRIVATE)
            prefs.edit() { putBoolean("agreed", false) }
            finishAffinity()
        }
    }
}
